#include "key.h"
#include "led.h"


void Key_Init(void)
{

	GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin : PA1 */
  GPIO_InitStruct.Pin = GPIO_PIN_1;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI1_IRQn);
	
}



void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{

    // HAL_GetTick() - tickstart) < wait

    static uint32_t last_tick = 0;

    uint32_t current_tick = HAL_GetTick();
    
    if (GPIO_Pin == GPIO_PIN_1)
    {
        if (current_tick - last_tick >200)
        {
            last_tick = current_tick;
            if(Led_Status==LED_ON) Led_Set(LED_OFF);
            else Led_Set(LED_ON);
        }
    }
}

